//
//  CPJMeituanRefreshControlView.swift
//  Sina
//
//  Created by YYY on 2017/8/23.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

class CPJMeituanRefreshControlView: CPJRefreshControlView
{
    
    //房子
    @IBOutlet weak var buildingIconView: UIImageView!
    
    //地球
    @IBOutlet weak var earthIconView: UIImageView!
    
    
    //袋鼠 
    @IBOutlet weak var kangarooIconView: UIImageView!
    
    //重写父类方法
    override var parentHeight: CGFloat
    {
        didSet
        {
            if parentHeight < 27
            {
                return
            }

            let tempScale: CGFloat = 1 - (123-parentHeight) / (123-27)
            
            let scale: CGFloat = (parentHeight > 123) ? 1 : tempScale
            kangarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
            //高度[27,100]
            //   (100-parentHeight) / (100-27)   [1,0]
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        //图片组动画
        imagesAnimation()
        
        //旋转动画
        rotateAnimation()
        
        //设置袋鼠的定位点
        kangarooLocation()
    }
    
    //图片组动画
    func imagesAnimation()
    {
        let image1 = #imageLiteral(resourceName: "房子1")
        let image2 = #imageLiteral(resourceName: "房子")
        buildingIconView.image = UIImage.animatedImage(with: [image1,image2], duration: 5)
        
        
    }
    //旋转动画
    func rotateAnimation()
    {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //动画完成后是否移除
        animation.isRemovedOnCompletion = false
        //旋转角度
        animation.toValue = -2 * Double.pi
        //重复次数
        animation.repeatCount = MAXFLOAT
        //动画持续时间
        animation.duration = 3.0
        earthIconView.layer.add(animation, forKey: nil)
    }
    
    
    //设置袋鼠的定位点
    func kangarooLocation()
    {
        //设置锚点
        kangarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        //设置中心点
        kangarooIconView.center = CGPoint(x: self.width * 0.5, y: self.height - 27)
        kangarooIconView.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
        
        //设置袋鼠动画组
        let kImage1 = #imageLiteral(resourceName: "袋鼠1")
        let kImage2 = #imageLiteral(resourceName: "袋鼠")
        kangarooIconView.image = UIImage.animatedImage(with: [kImage1,kImage2], duration: 2)
    }
}
